﻿
Partial Class Interfaz_frmBodegaSalidaMateriales
    Inherits System.Web.UI.Page
    Dim CL As New Logica.ModuloBodega.ControladorLogica

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CL = Logica.ModuloBodega.ControladorLogica.getInstance

        If CL.visibilidadesFormsInternos("Bodega", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim ArrayMaterial As New ArrayList
        Dim i As Integer
        ArrayMaterial = CL.ObtenerMaterial()

        If Not Page.IsPostBack Then
            For i = 0 To ArrayMaterial.Count - 1
                Dim list As New ListItem

                list.Text = DirectCast(ArrayMaterial.Item(i), Material).Nombre
                list.Value = DirectCast(ArrayMaterial.Item(i), Material).ID

                Me.DropItem.Items.Add(list)
            Next
        End If
    End Sub

#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub insertarBD()

        Dim fecha As String = Calendario1.obtieneValorFecha
        Dim IdMaterial As String = Me.DropItem.SelectedValue
        Dim EntregadoPor As String = TxtSolicitante.Text
        Dim retiradoPor As String = Me.TxtSolicitante.Text
        Dim AprobadoPor As String = Me.TxTNombreAprobador.Text
        Dim CantidadRetirada As String = Me.TxtCantDespacho.Text

        If fecha = "" Then
            Me.Mensaje("Debe Ingresar fecha de salida del material")
        Else
            If EntregadoPor = "" Then
                Me.Mensaje("Debe ingresar destinatario de material")
            Else
                If CantidadRetirada = "" Then
                    Me.Mensaje("Debe ingresarCantidad Retirada")
                Else

                    Dim materialSalida As New MaterialSalida

                    materialSalida.Nombre = Me.DropItem.Text
                    materialSalida.FechaSalida = Calendario1.obtieneValorFecha
                    materialSalida.DespachadoPor = EntregadoPor
                    materialSalida.AprovadoPor = AprobadoPor
                    materialSalida.RetiradoPor = retiradoPor
                    materialSalida.CantRetirada = CantidadRetirada

                    CL.ActualizarMaterialSalida(materialSalida, IdMaterial)
                    Me.Mensaje("El material a sido descontado exitosamente")
                End If
            End If
        End If
        Clean()
    End Sub

    Private Sub Clean()
        Me.TxtSolicitante.Text = ""
        Me.TxtCantDespacho.Text = ""
    End Sub
#End Region

#Region "Botones"

    Protected Sub BtnDescontar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDescontar.Click
        Me.insertarBD()
    End Sub

    Protected Sub BtnClean_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnClean.Click
        Me.Clean()
    End Sub
#End Region

    
    
End Class
